﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.BusinessLogic;
using Payroll.Common.Utils;
using Payroll.Common.Utils.Html;
using PayrollHR.Web;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Directory;
using Payroll.BusinessLogic.Utils;
using System.IO;
using OfficeOpenXml;
using Payroll.Common;
using Payroll.BusinessLogic.Devices;
using Payroll.BusinessLogic.Package;

namespace PayrollHR.Administration.Modules
{
    public partial class VoucherControl : BasePayrollAdministrationUserControl
    {
        private const string m_filetemplate = "CustomerList.xlsx";
        private const int m_pageSize = 50;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }
        protected void BindGrid()
        {
            var voucher = GetVouchers();
            gvCustomers.DataSource = voucher;
            gvCustomers.DataBind();
        }
        protected List<Voucher> GetVouchers()
        {
            var voucherSearch = ctrVoucherSearch.GetVoucherSearch();
            PagedList<Voucher> voucher = VoucherService.GetAllVoucher(voucherSearch.StartTime, voucherSearch.EndTime, voucherSearch.Taxcode, voucherSearch.CompanyName, voucherSearch.Vendor, null, voucherSearch.Status);
            return voucher;
        }
        protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCustomers.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        #region btn
        protected void SearchButton_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }     
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    List<int> customerColection = new List<int>();
                    foreach (GridViewRow row in gvCustomers.Rows)
                    {
                        CheckBox cbCustomerInfo = row.FindControl("cbCustomerInfo") as CheckBox;
                        HiddenField hfCustomerID = row.FindControl("hfCustomerID") as HiddenField;
                        int CustomerID = int.Parse(hfCustomerID.Value);
                        if (cbCustomerInfo.Checked)
                        {
                            customerColection.Add(CustomerID);
                        }
                    }
                    if (customerColection.Count > 0)
                    {
                        this.CustomerService.Delete(customerColection);
                        this.BindGrid();
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }
        #endregion

        protected void btnExport_Click(object sender, EventArgs e)
        {
            string fileName = string.Format("TemplateImport{0}", DateTime.Now.ToString("dd-MM-yyyy_HH.mm") + ".xlsx");           
            FileInfo newFile = new FileInfo(Server.MapPath(@"..\Administration\FileExport\" + fileName));
            FileInfo template = new FileInfo(Server.MapPath(@"..\Administration\Template\templateVoucher.xlsx"));            
            writeSheets(newFile, template);
            hlexport.Visible = true;
            hlexport.Text = "Save As File Export";
            hlexport.NavigateUrl = "../FileExport/" + fileName;    
        }
        private void writeSheets(FileInfo newFile, FileInfo template)
        {
            using (var xlPackage = new ExcelPackage(newFile, template))
            {
                ExcelWorksheet sheetPackete = xlPackage.Workbook.Worksheets[2];
                ExcelWorksheet sheetNCC = xlPackage.Workbook.Worksheets[3];
                this.SheetPacket(sheetPackete);
                this.SheetNPP(sheetNCC);
                xlPackage.Save();
            }
        }
        public PackageSearch GetPackageSearch()
        {
            DateTime? date = DateTime.Now;
            var item = new PackageSearch
            {
                UserId = PayrollContext.Current.Session.EmployeeId,
                PackName =null ,
                PriceDate = date
            };
            return item;
        }
        private void SheetPacket(ExcelWorksheet sheetdata)
        {
            var packetColection = this.PackageService.GetAllPackagePrice(GetPackageSearch(), m_pageSize);
            int i = 2;
            foreach(var info in packetColection)
            {
                sheetdata.Cells[i, 1].Value = i - 1;
                sheetdata.Cells[i, 2].Value = info.PackCode;
                sheetdata.Cells[i, 3].Value = info.Name;
                sheetdata.Cells[i, 4].Value = info.PackTypeName;
                sheetdata.Cells[i, 5].Value = CommonHelper.ToDec(info.Price);
                i++;
            }
            CommonHelper.BorderCell(sheetdata, 2, packetColection.Count + 1, 1, 5);
        }
        private void SheetNPP(ExcelWorksheet sheetdata)
        {
            int i = 2;
            var customerColection = this.CustomerService.GetAllCustomer();
            foreach (var info in customerColection)
            {
                sheetdata.Cells[i, 1].Value = i - 1;
                sheetdata.Cells[i, 2].Value = info.CustCode;
                sheetdata.Cells[i, 3].Value = info.CustName;
                sheetdata.Cells[i, 4].Value = info.CustAddress;
                i++;
            }
            CommonHelper.BorderCell(sheetdata, 2, customerColection.Count + 1, 1, 4);
        }

        protected void btnLock_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    List<int> voucherColection = new List<int>();
                    foreach (GridViewRow row in gvCustomers.Rows)
                    {
                        CheckBox cbCustomerInfo = row.FindControl("cbVoucherInfo") as CheckBox;
                        HiddenField hfVoucherID = row.FindControl("hfVoucherID") as HiddenField;
                        int VoucherID = int.Parse(hfVoucherID.Value);
                        if (cbCustomerInfo.Checked)
                        {
                            voucherColection.Add(VoucherID);
                        }
                    }
                    if (voucherColection.Count > 0)
                    {
                        this.VoucherService.StatusUpdate(voucherColection);
                        this.BindGrid();
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }
    }
}